<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\AppOrder;
use App\Models\Information;
use App\Models\InformationCollection;
use App\Models\InformationLike;
use App\Models\InformationType;
use App\Models\VipOrder;
use Illuminate\Http\Request;

class InformationController extends Controller
{

    public function index(Request $Request)
    {
        $search = $Request->search;
        $type = $Request->type;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Information::select('id','title','img','click','create_time')->withCount('likes')->when($search, function ($query, $search) {
                    return $query->where('title','like', $search.'%');
                })
                ->when($type, function ($query, $type) {
                    return $query->where('type', $type);
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function types()
    {
        $data = InformationType::get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function info(Request $Request)
    {
        $data = Information::withCount('likes')->withCount('collects')->where('id',$Request->id)->first();
        if(!$data){
            return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
        }
        $data->content = str_replace('src="/ueditor/php/upload','src="https://gl.xinzhuvip.com/ueditor/php/upload',$data->content);
        //判断是否购买过
        $data->auth = false;
        if($Request->uid){
            $order = AppOrder::select('id')->where('user_id',$Request->uid)->where('product_id',$Request->id)->where('type','information')->where('pay_status','success')->first();
            if($order){
                $data->auth = true;
            }
    
            //是否是vip
            $vip = VipOrder::with('vip')->where('uid',$Request->uid)->where('status',1)->orderBy('vip_id','desc')->first();
            $data->vip = [];
            if($vip){
                $data->auth = true;
                $data->vip = $vip->vip->auth;
            }
        }
        

        $data->increment('click');
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }


    public function like(Request $Request)
    {
        $data = InformationLike::where('uid',$Request->uid)->where('nid',$Request->id)->first();
        if($data){
            $data->delete();
        }else{
            $data = new InformationLike();
            $data->nid = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function collect(Request $Request)
    {
        $data = InformationCollection::where('uid',$Request->uid)->where('nid',$Request->id)->first();
        if($data){
            $data->delete();
        }else{
            $data = new InformationCollection();
            $data->nid = $Request->id;
            $data->uid = $Request->uid;
            $data->create_time = time();
            $data->update_time = time();
            $data->save();
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
}
